package com.information.manage.basic.common.utils.zip;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class ZipUtils {

    /**
     * 导出zip模板
     * @param response
     * @param path resources下的模板路径
     */
    public static void downloadZipTemplate(HttpServletResponse response, String path) {
        InputStream stream = ZipUtils.class.getResourceAsStream(path);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "blob");
        try {
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode("导入模板.zip", "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            System.out.println("设置响应参数错误" + e);
        }
        try(OutputStream outputStream = response.getOutputStream()){
            int count = 0;
            byte[] buffer = new byte[2048];
            while((count = stream.read(buffer)) != -1){
                outputStream.write(buffer, 0, count);
            }
        }catch (IOException e){
            System.out.println("导入模板错误。" + e);
        }
    }

}
